<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class order extends CI_Controller {

    function __construct() {

        parent::__construct();
        $this->load->helper('url');
        $this->load->library(array('form_validation', 'session'));
        $this->load->library('adminlayout');
        $this->adminlayout->setLayout("admin/layout");
        $userSession = $this->session->userdata('user');

        if ($userSession['username'] == false) {
            redirect(base_url('/index/login?returnUrl=') . urlencode(RETURN_URL));
        } else {
            if ($userSession['useradmin'] != 1) {
                redirect(base_url('nhanvien/event/list_event'));
            }
        }
    }

    function listOrder() {

        $title = "Danh sach ngay dat com";
        $data['abc'] = null;
        $this->adminlayout->view("admin/list_order", $data, $title);
    }

    //lay danh sach dat com trong mot ngay nao do ok
    //lấy danh sách đặt cơm trong một ngày nào đó ok
    function getDetail1() {
        $this->load->helper('date');
        $dayId = $this->input->get('dayId');
        if ($dayId !== false) {

            $this->load->model("admin/order_model");
            $result = $this->order_model->getDetailOrderDay($dayId);
            $resultd = $this->order_model->getDay($dayId);
            if(count($result) > 0){
                $data['query'] = $result;
                $title = $result[0]->HoTen;
            }
            
            if(count($resultd) > 0){
                $data['day'] = $resultd[0]->NgayDatCom;
            }
            $data['ID']=$dayId;
                //<<<<<<<<<<<<<<HoTen
            $this->adminlayout->view("admin/dayOrderDetail", $data, 'Danh sách đặt cơm ngày');
            return  $data;
        }
        
    }

    function getDetailView() {
        $title = "Danh sach dat com";
        $data['abc'] = null;
        $this->adminlayout->view("admin/dayOrderDetail", $data, $title);
    }

    function getDetail() {
        $data['sort'] = $_GET["jtSorting"];
        $data['startIndex'] = $_GET["jtStartIndex"];
        $data['pageSize'] = $_GET["jtPageSize"];
        $this->load->model("admin/order_model");
        $result = $this->order_model->getDetailOrderDay($dayId);

        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['TotalRecordCount'] = $result['recordCount'];
        $jTableResult['Records'] = $result['query'];
        print json_encode($jTableResult);
    }

    function getList() {
        $data['sort'] = $_GET["jtSorting"];
        $data['startIndex'] = $_GET["jtStartIndex"];
        $data['pageSize'] = $_GET["jtPageSize"];
        $this->load->model("admin/order_model");
        $result = $this->order_model->getList($data);
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['TotalRecordCount'] = $result['recordCount'];
        $jTableResult['Records'] = $result['query'];
        print json_encode($jTableResult);
    }

    //thêm ngày đặt cơm
    //them ngay dat com
    function addDay() {
        $data['MaNgayDatCom']=NULL;
        if(!empty($_POST['NgayDatCom'])){
            $data['NgayDatCom'] = $_POST['NgayDatCom'];
            $data['HanGioDatCom'] = $_POST['HanGioDatCom'];
            $this->load->model("admin/order_model");
            $result = $this->order_model->insertDay($data);
            $jTableResult = array();
            $jTableResult['Result'] = "OK";
            $jTableResult['Record'] = $result;
            print json_encode($jTableResult);
        }else{
            $jTableResult = array();
            $jTableResult['Result'] = "ERROR";
            $jTableResult['Message'] = "Nhập Ngày Đặt Cơm";
            print json_encode($jTableResult);
        }
        
    }

    function page() {
        return true;
    }

    public function excel(){
//        $this->adminlayout->view("demo_php_tao_excel_xls", null, null);
        $this->adminlayout->view("index", null, null);
        
    }
    
    
      function deleteDay(){
        try{
        if(isset($_POST["MaNgayDatCom"])){
            $id = $_POST["MaNgayDatCom"];
            $this->load->model("admin/order_model");
            $this->order_model->deleteDay1($id);
            $jTableResult = array();
            $jTableResult['Result'] = "OK";
            print json_encode($jTableResult);
            
        }
        }catch(Exception $ex){
            $jTableResult = array();
	        $jTableResult['Result'] = "ERROR";
	           $jTableResult['Message'] = $ex->message();
    	       print json_encode($jTableResult);
        }
        
    }
    
    function updateDay(){
        if(!empty($_POST['NgayDatCom'])){
            $data['MaNgayDatCom'] = $_POST['MaNgayDatCom'];
            $data['NgayDatCom'] = $_POST['NgayDatCom'];
            $data['HanGioDatCom'] = $_POST['HanGioDatCom'];
            
            $this->load->model("admin/order_model");
            $this->order_model->updateDay1($data);
    		$jTableResult['Result'] = "OK";
    		print json_encode($jTableResult);
        }else{
            $jTableResult = array();
            $jTableResult['Result'] = "ERROR";
            $jTableResult['Message'] = "Nhập Ngày Đặt Cơm";
            print json_encode($jTableResult);
        }
    }
    
    
    function file_xls1() {
        $this->load->helper('date');
        $dayId = $this->input->get('dayId');
        if ($dayId !== false) {
//            file_xls('55') ;
            $this->file_xls( ) ;
            
        }
    }

     function file_xls(  ) {
         $t=2;
//        $que=$this->input->get('query');
        $iid = $this->input->get('id');
         $this->load->helper('date');
        //////////////////////////////////////////
            $this->load->library('phpexcel/PHPExcel'); 
        $sheet = $this->phpexcel->getActiveSheet();
        if ($iid !== false) {
            $this->load->helper('url');
            $this->load->library(array('form_validation', 'session'));
            $this->load->database();

            $query = $this->db->query(" select hosonhanvien.MaHoSo, HoTen, SoDienThoai from datcom, datcomnhanvien, hosonhanvien where hosonhanvien.MaHoSo=datcomnhanvien.MaHoSo and datcom.MaNgayDatCom = datcomnhanvien.MaNgayDatCom and datcom.MaNgayDatCom= '$iid'");
            if ($query) {
                $data['que'] = $query->result();
            }
            
             $sheet->setCellValue('A1', 'Danh sách nhân viên đặt cơm'); 
             $sheet->setCellValue('A2', 'Mã Hồ Sơ'); 
             $sheet->setCellValue('B2', 'Họ và Tên'); 
             $sheet->setCellValue('C2', 'Số điện thoại'); 

            foreach($data['que'] as $row) {
                $t++;;
                $sheet->setCellValue('A'.$t,  $row->MaHoSo);
                $sheet->setCellValue('B'.$t,  $row->HoTen);
                $sheet->setCellValue('C'.$t,  $row->SoDienThoai);
                 
                
            }







        
        $sheet->getColumnDimension('A')->setWidth(5);
//        $sheet->setCellValue('A' . '1', $iid);
//        $sheet->setCellValue('A2', 'Danh sách'); 
        $writer = new PHPExcel_Writer_Excel5($this->phpexcel);
        header('Content-type: application/vnd.ms-excel');
        $writer->save('php://output');
    }
 
}
}
?>
